<!DOCTYPE html>
<html>
<head>
    <title>微信机器人监控面板</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <style>
        body {
            background-color: #f0f2f5;
            height: 100vh;
            margin: 0;
            padding: 20px;
        }
        .chat-container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            height: calc(100vh - 40px);
            display: flex;
        }
        .sidebar {
            width: 300px;
            border-right: 1px solid #e4e4e4;
            padding: 20px;
            display: flex;
            flex-direction: column;
        }
        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            position: relative;
        }
        .chat-header {
            padding: 20px;
            border-bottom: 1px solid #e4e4e4;
            background: white;
            border-radius: 15px 15px 0 0;
        }
        .chat-messages {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .message-bubble {
            max-width: 70%;
            margin-bottom: 20px;
            clear: both;
        }
        .message-user {
            float: left;
        }
        .message-bot {
            float: right;
        }
        .message-content {
            padding: 12px 16px;
            border-radius: 15px;
            position: relative;
            word-wrap: break-word;
        }
        .message-user .message-content {
            background: white;
            margin-left: 45px;
        }
        .message-bot .message-content {
            background: #95ec69;
            margin-right: 45px;
        }
        .avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            position: absolute;
            bottom: 0;
        }
        .message-user .avatar {
            left: 0;
        }
        .message-bot .avatar {
            right: 0;
        }
        .message-info {
            font-size: 0.8em;
            color: #999;
            margin: 5px 45px;
        }
        .message-user .message-info {
            text-align: left;
        }
        .message-bot .message-info {
            text-align: right;
        }
        .stats-container {
            padding: 15px;
            background: #f8f9fa;
            border-radius: 10px;
            margin-bottom: 20px;
        }
        .stats-item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
        }
        .user-list {
            flex: 1;
            overflow-y: auto;
        }
        .user-item {
            padding: 10px;
            border-radius: 10px;
            margin-bottom: 10px;
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
        }
        .user-item:hover {
            background-color: #f0f2f5;
            transform: translateX(5px);
        }
        .user-item.active {
            background-color: #e3f2fd;
            transform: translateX(5px);
        }
        .user-item img {
            transition: transform 0.2s;
        }
        .user-item:hover img {
            transform: scale(1.1);
        }
        .refresh-button {
            padding: 8px 15px;
            background: #1a73e8;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .refresh-button:hover {
            background: #1557b0;
        }
        .status-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            display: inline-block;
            margin-right: 5px;
        }
        .status-online {
            background-color: #4caf50;
        }
        .empty-state {
            text-align: center;
            color: #666;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="sidebar">
            <div class="stats-container">
                <h5><i class="bi bi-graph-up"></i> 统计信息</h5>
                <div class="stats-item">
                    <span>总消息数</span>
                    <span id="total-messages">0</span>
                </div>
                <div class="stats-item">
                    <span>活跃用户</span>
                    <span id="active-users">0</span>
                </div>
                <div class="stats-item">
                    <span>今日消息</span>
                    <span id="today-messages">0</span>
                </div>
            </div>
            <h5><i class="bi bi-people"></i> 聊天列表</h5>
            <div class="user-list" id="user-list">
                <!-- 用户列表将在这里动态加载 -->
            </div>
        </div>
        
        <div class="main-content">
            <div class="chat-header">
                <div class="d-flex justify-content-between align-items-center">
                    <h4 class="mb-0">
                        <span class="status-indicator status-online"></span>
                        <span id="current-user">所有用户</span>
                    </h4>
                    <button class="refresh-button" onclick="updateMessages()">
                        <i class="bi bi-arrow-clockwise"></i> 刷新
                    </button>
                </div>
                <div class="text-muted small">
                    上次更新: <span id="last-update">-</span>
                </div>
            </div>
            
            <div class="chat-messages" id="messages">
                <!-- 消息将在这里动态加载 -->
            </div>
        </div>
    </div>

    <script>
        let activeUsers = new Set();
        let todayMessages = 0;
        let selectedUser = null;

        function formatTime(timeStr) {
            const date = new Date(timeStr);
            return date.toLocaleString();
        }

        function isToday(timeStr) {
            const date = new Date(timeStr);
            const today = new Date();
            return date.getDate() === today.getDate() &&
                   date.getMonth() === today.getMonth() &&
                   date.getFullYear() === today.getFullYear();
        }

        function filterMessagesByUser(user) {
            selectedUser = user;
            updateMessages();
            
            // 更新用户列表选中状态
            document.querySelectorAll('.user-item').forEach(item => {
                item.classList.remove('active');
                if (item.getAttribute('data-user') === user) {
                    item.classList.add('active');
                }
            });
            
            // 更新标题
            document.getElementById('current-user').textContent = user ? user : '所有用户';
        }

        function updateMessages() {
            fetch('/messages')
                .then(response => response.json())
                .then(data => {
                    const container = document.getElementById('messages');
                    container.innerHTML = '';
                    activeUsers.clear();
                    todayMessages = 0;
                    
                    // 过滤消息
                    let filteredMessages = selectedUser ? 
                        data.messages.filter(msg => msg.sender_name === selectedUser) :
                        data.messages;
                    
                    filteredMessages.forEach(msg => {
                        // 更新统计信息
                        activeUsers.add(msg.sender_name);
                        if (isToday(msg.created_at)) {
                            todayMessages++;
                        }

                        // 添加消息气泡
                        container.innerHTML += `
                            <div class="message-bubble message-user">
                                <img src="https://api.dicebear.com/6.x/initials/svg?seed=${msg.sender_name}" class="avatar" alt="user">
                                <div class="message-content">
                                    ${msg.message}
                                </div>
                                <div class="message-info">
                                    ${msg.sender_name} · ${msg.created_at}
                                </div>
                            </div>
                            <div class="message-bubble message-bot">
                                <img src="https://api.dicebear.com/6.x/bottts/svg?seed=bot" class="avatar" alt="bot">
                                <div class="message-content">
                                    ${msg.reply}
                                </div>
                                <div class="message-info">
                                    机器人 · ${msg.created_at}
                                </div>
                            </div>
                        `;
                    });

                    // 更新统计信息
                    document.getElementById('total-messages').textContent = data.messages.length;
                    document.getElementById('active-users').textContent = activeUsers.size;
                    document.getElementById('today-messages').textContent = todayMessages;
                    
                    // 更新用户列表
                    const userList = document.getElementById('user-list');
                    userList.innerHTML = `
                        <div class="user-item ${!selectedUser ? 'active' : ''}" 
                             onclick="filterMessagesByUser(null)"
                             data-user="all">
                            <img src="https://api.dicebear.com/6.x/initials/svg?seed=all" 
                                 style="width: 30px; height: 30px; border-radius: 50%; margin-right: 10px;">
                            所有用户
                        </div>
                    `;
                    
                    activeUsers.forEach(user => {
                        userList.innerHTML += `
                            <div class="user-item ${selectedUser === user ? 'active' : ''}"
                                 onclick="filterMessagesByUser('${user}')"
                                 data-user="${user}">
                                <img src="https://api.dicebear.com/6.x/initials/svg?seed=${user}" 
                                     style="width: 30px; height: 30px; border-radius: 50%; margin-right: 10px;">
                                ${user}
                            </div>
                        `;
                    });

                    // 更新时间
                    document.getElementById('last-update').textContent = new Date().toLocaleString();
                    
                    // 滚动到最新消息
                    container.scrollTop = container.scrollHeight;
                })
                .catch(error => {
                    console.error('获取消息失败:', error);
                });
        }

        // 页面加载时立即更新
        updateMessages();

        // 每30秒自动更新一次
        setInterval(updateMessages, 30000);
    </script>
</body>
</html> 